#!/bin/sh
#作者：高伟
#my email: xiyou.gaowei@gmail.com
#version: IPv6G 0.1

#用法说明
usage()
{
	echo "Today's date is `date`"
	echo "The software's usage"
	echo "./IPv6G IPv6"
}

#IPv6前缀产生的策略的选择
ipv6()
{
	echo "请选择策略，A或者B，首选为B"
	read scheme
	if [ "$scheme" = "B" ]; then
		B
	else
		A
	fi
}

#IPv6前缀产生B策略
B() 
{
echo "首选B策略"
echo "ASN+IPv4前缀+随机数"
asn=`expr 1024 "*" 8`
o=0

echo "请输入一个IPv4前缀,如202.117.128.0/20"
#IPv6前缀长度等于IPv4长度的2倍
read IPv4P

#把IPv4前缀分割成5个部分
ip1=`echo "$IPv4P" | cut -f 1 -d "."`
ip2=`echo "$IPv4P" | cut -f 2 -d "."`
ip3=`echo "$IPv4P" | cut -f 3 -d "."`
ip5=`echo "$IPv4P" | cut -f 4 -d "."`
ip4=`echo "$ip5" | cut -f 1 -d "/"`
IPv4L=`echo "$ip5" | cut -f 2 -d "/"`

#把上面的5个部分，从10进制转化为2进制
IP1=`echo "obase=2; ibase=10; $ip1"|bc`
IP2=`echo "obase=2; ibase=10; $ip2"|bc`
IP3=`echo "obase=2; ibase=10; $ip3"|bc`
IP4=`echo "obase=2; ibase=10; $ip4"|bc`

P=($IP1 $IP2 $IP3 $IP4)
p=(i1 i2 i3 i4)

IPv6L=`expr $IPv4L "*" 2`
randomL=`expr $IPv4L - 16`
echo "自动或手动输入一个 $randomL bit的随机值，自动则输入a即可"
read random
#生成一个随机数
if [ "$random" = "a" ]; then
	r_a=(0 1)
	random=`for ((i=0;i<$randomL;i++)); do echo -n ${r_a[$RANDOM % ${#r_a[*]}]}; done`
fi

#把IPv4前缀转换为一个2进制的字符串

for ((i=0;i<4;i++));do
        l=${#P[i]}
        if [ "$l" -le "8" ]; then
                zr_number=`expr 8 - $l`
                a=${P[i]}
                while [ "$zr_number" -gt "$o" ]; do
                        a=0$a
                        zr_number=`expr $zr_number - 1`
                done
        fi
        p[i]=$a
done
IPv4_prefix=`echo "${p[0]}${p[1]}${p[2]}${p[3]}"`
#echo "$IPv4_prefix"

prefix_random=`echo "$IPv4_prefix$random"`

#实现asn的值
while [ "$asn" -ge "$o" ]; do
s=$asn
i=`echo "obase=2; $asn" | bc`
iL=`echo $i|awk '{print length($i)}'`

        if [ "$iL" -lt "16" ]
        then
                z_number=`expr 16 - $iL`
                j=$i
                while [ "$z_number" -gt "$o" ]; do
                        j=0$j
                        z_number=`expr $z_number - 1`
                done
        fi

jL=`echo $j|awk '{print length($j)}'`

L1=`echo $j|awk '{print substr($j,1,4)}'`
L2=`echo $j|awk '{print substr($j,5,4)}'`
L3=`echo $j|awk '{print substr($j,9,4)}'`
L4=`echo $j|awk '{print substr($j,13,4)}'`

l1=`echo "obase=16; ibase=2; $L1"|bc`
l2=`echo "obase=16; ibase=2; $L2"|bc`
l3=`echo "obase=16; ibase=2; $L3"|bc`
l4=`echo "obase=16; ibase=2; $L4"|bc`

ASN=`echo "$l1$l2$l3$l4"`
asn=`expr $s - 1`

#echo "$ASN:$IPv4P:$random/$IPv6L"
echo "$ASN:$prefix_random/$IPv6L"

done
		
}

#IPv6前缀产生的A策略
A()
{
	echo "A group"
}

#目前不知道如何写
ipv4()
{
	echo "IPv4 perfix"
#创建存储表的文件
	if [ -e yiyi ]; then
		echo "已经有yiyi"
		echo "是否删除原来的yiyi，yes或者no"
		read y_or_n
		case "$y_or_n" in
			y*) rm yiyi
			    echo "已经删除yiyi";;
			n*) echo "no";;
		esac
	else
		touch yiyi
	fi
#创建表	
	echo 请输入i的值
	read i
	j=0

	while [ "$j" -lt "$i" ]; do
		echo -e $j >> yiyi
		j=`expr $j + 1`
	done
}

case "$1" in
	IPv4) echo "IPv4 table"
	      ipv4;;
	IPv6) echo "IPv6 table"
	      ipv6;;
	* | h* | H*)    usage;;
esac

exit 0
